package com.raja.servingStation.controller.engineer;

import com.alibaba.fastjson.JSONObject;
import com.raja.servingStation.common.constant.MysqlPageVo;
import com.raja.servingStation.common.util.RequestUtil;
import com.raja.servingStation.common.util.ResponseUtil;
import com.raja.servingStation.controller.common.BaseController;
import com.raja.servingStation.service.EngineerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by zhangzhen
 * on 2017/10/10.
 */
@RestController
public class EngineerController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(EngineerController.class);
    @Autowired
    private ResponseUtil responseUtil;
    @Autowired
    private EngineerService engineerService;


    /**
     * 工程师列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/engineer/list",method = RequestMethod.GET)
    public ResponseEntity list(HttpServletRequest request) {
        try {
            JSONObject body = RequestUtil.parseBody(request);
            MysqlPageVo pageVO = engineerService.queryOrderListByPage(body);
            return responseUtil.success(pageVO);
        }catch (Exception e) {
            log.error(e.getMessage(), e);
            return responseUtil.exception("查询列表异常");
        }
    }

    /**
     * 所属工程师的工单列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/engineer/getOrdersByEngineerId",method = RequestMethod.GET)
    public ResponseEntity getOrdersByEngineerId(HttpServletRequest request) {
        try {
            JSONObject body = RequestUtil.parseBody(request);
            Integer stationId = RequestUtil.getUserId(request);
            stationId = 1;
            MysqlPageVo pageVO = engineerService.getOrdersByEngineerId(body,stationId);
            return responseUtil.success(pageVO);
        }catch (Exception e) {
            log.error(e.getMessage(), e);
            return responseUtil.exception("查询列表异常");
        }
    }

    /**
     * 新增工程师
     * @param request
     * @return
     */
    @RequestMapping(value = "/engineer/add",method = RequestMethod.GET)
    public ResponseEntity add(HttpServletRequest request) {
        try {
            JSONObject body = RequestUtil.parseBody(request);
            Object id = engineerService.addEngineer(body);
            return responseUtil.success(id);
        }catch (Exception e) {
            log.error(e.getMessage(), e);
            return responseUtil.exception("新增工程师异常");
        }
    }


}
